package com.example.demo.controller;


import com.example.demo.Service.CommentService;
import com.example.demo.Service.LikeService;
import com.example.demo.Service.PostService;
import com.example.demo.entity.Comment;
import com.example.demo.entity.Like;
import com.example.demo.entity.Post;
import com.example.demo.entity.User;
import com.example.demo.repository.CommentRepository;
import com.example.demo.repository.LikeRepository;
import com.example.demo.repository.PostRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/likes")
public class LikeController {
    @Autowired
    private LikeService likeService;

    @Autowired
    private LikeRepository likeRepository;

    @Autowired
    private PostService postService;

    @Autowired
    private CommentService commentService;

    @Autowired
    private PostRepository postRepository;

    @Autowired
    private CommentRepository commentRepository;

    //点赞
    @PostMapping
    public ResponseEntity<?> createLike(@RequestBody Like like) {
        try {
            //检查点赞对象类型并进行相应的处理
            if (like.getPost() != null) {
                //点赞对象是Post
                Post post = postRepository.findById(like.getPost().getId())
                        .orElseThrow(() -> new IllegalArgumentException("Post not found"));

                //关联找到的Post到Like对象
                like.setPost(post);
            } else if (like.getComment() != null) {
                //点赞对象是Comment
                Comment comment = commentRepository.findById(like.getComment().getId())
                        .orElseThrow(() -> new IllegalArgumentException("Comment not found"));

                //关联找到的Comment到Like对象
                like.setComment(comment);
            } else {
                //如果既不是对Post也不是对Comment的点赞，则抛出异常
                throw new IllegalArgumentException("Like must be for a post or a comment.");
            }

            //保存Like对象
            Like savedLike = likeService.save(like);
            return ResponseEntity.ok(savedLike);
        } catch (IllegalArgumentException e) {
            //返回错误响应
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage());
        } catch (Exception e) {
            //捕获其他错误
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("An error occurred while saving the like.");
        }
    }

    //取消点赞
    @DeleteMapping("/{id}")
    public void deleteLike(@PathVariable Long id){
        likeService.deleteById(id);
    }

    //获取文章的点赞数
    @GetMapping("/post/count/{postid}")
    public Long countLikesByPost(@PathVariable Long postid) {
        Post post = new Post();
        post.setId(postid);
        if (post == null || post.getId() == null) {
            throw new IllegalArgumentException("Post not found");
        }
        return likeService.countLikesByPost(post);
    }

    //获取评论的点赞数
    @GetMapping("/comment/{commentId}/count")
    public Long countLikesByComment(@PathVariable Long commentId) {
        Comment comment = new Comment();
        comment.setId(commentId);
        if (commentId == null) {
            throw new IllegalArgumentException("Comment not found");
        }
        return likeService.countLikesByComment(comment);
    }

    //检查用户是否已经对文章点赞
    @GetMapping("/post/{postId}/user/{userId}/hasLiked")
    public boolean hasLikedPost(@PathVariable Long postId, @PathVariable Long userId) {
        Post post = new Post();
        post.setId(postId);

        User user = new User();
        user.setId(userId);

        return likeRepository.existsByPostAndUser(post, user);
    }

    //检查用户是否已经对评论点赞
    @GetMapping("/comment/{commentId}/user/{userId}/hasLiked")
    public boolean hasLikedComment(@PathVariable Long commentId, @PathVariable Long userId) {
        Comment comment = new Comment();
        comment.setId(commentId);

        User user = new User();
        user.setId(userId);

        return likeRepository.existsByCommentAndUser(comment, user);
    }
}
